/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.device.servlet;

import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * 删除设备实例
 *
 * @author LiuShang
 */
@WebServlet(name = "cn.net.greenet.device.servlet.Delete", urlPatterns = {"/device/delete"})
public class Delete extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("id");
        if (id != null && id.matches("[0-9]*")) {
            PrintWriter out = response.getWriter();
            SessionFactory sf = HibernateUtil.getSessionFactory();
            Session session = sf.openSession();
            Transaction tx = session.beginTransaction();

            try {
                int did = Integer.parseInt(id);
//                Dpiendpointbean bean = (Dpiendpointbean) session.get(Dpiendpointbean.class, Integer.parseInt(id));

                // delete Ackoutgoinglog set
                Query deleteAckincominglogs = session.createQuery("delete Ackincominglog as c where c.dpiendpointbean.id=:did");
                deleteAckincominglogs.setParameter("did", did);
                deleteAckincominglogs.executeUpdate();

                // delete Ackoutgoinglog set
                Query deleteAckoutgoinglogs = session.createQuery("delete Ackoutgoinglog as c where c.dpiendpointbean.id=:did");
                deleteAckoutgoinglogs.setParameter("did", did);
                deleteAckoutgoinglogs.executeUpdate();

                // delete Currentmessagesequencenos set
                Query deleteCurrentmessagesequencenos = session.createQuery("delete Currentmessagesequenceno as c where c.id.dpiendPointBeanId=:did");
                deleteCurrentmessagesequencenos.setParameter("did", did);
                deleteCurrentmessagesequencenos.executeUpdate();

                // delete Messageincominglogs
                Query deleteMessageincominglogs = session.createQuery("delete Messageincominglog as c where c.dpiendpointbean.id=:did");
                deleteMessageincominglogs.setParameter("did", did);
                deleteMessageincominglogs.executeUpdate();

                // delete MessageoutgoinglogManuals
                Query deleteMessageoutgoinglogManuals = session.createQuery("delete MessageoutgoinglogManual as c where c.dpiendpointbean.id=:did");
                deleteMessageoutgoinglogManuals.setParameter("did", did);
                deleteMessageoutgoinglogManuals.executeUpdate();

                // delete Messageoutgoinglogs
                Query deleteMessageoutgoinglogs = session.createQuery("delete Messageoutgoinglog as c where c.dpiendpointbean.id=:did");
                deleteMessageoutgoinglogs.setParameter("did", did);
                deleteMessageoutgoinglogs.executeUpdate();

                // delete Messageoutgoingsendingqueue
                Query deleteMessageoutgoingsendingqueues = session.createQuery("delete Messageoutgoingsendingqueue as c where c.dpiendpointbean.id=:did");
                deleteMessageoutgoingsendingqueues.setParameter("did", did);
                deleteMessageoutgoingsendingqueues.executeUpdate();

                // delete Messageoutgoingsendingqueue
                Query deletePolicysynchronizations = session.createQuery("delete Policysynchronization as c where c.dpiendpointbean.id=:did");
                deletePolicysynchronizations.setParameter("did", did);
                deletePolicysynchronizations.executeUpdate();

                // delete Versionmanaged
                Query deleteVersionmanageds = session.createQuery("delete Versionmanaged as c where c.dpiendpointbean.id=:did");
                deleteVersionmanageds.setParameter("did", did);
                deleteVersionmanageds.executeUpdate();

                // delete Versionreported
                Query deleteVersionreporteds = session.createQuery("delete Versionreported as c where c.dpiendpointbean.id=:did");
                deleteVersionreporteds.setParameter("did", did);
                deleteVersionreporteds.executeUpdate();

                // delete Messageoutgoingsendingqueue
                Dpiendpointbean bean = (Dpiendpointbean) session.load(Dpiendpointbean.class, did);
                bean.getPolicyrepositoryinstances().clear();
                session.merge(bean);

                session.delete(bean);

                tx.commit();
                try {
                    /* TODO output your page here. You may use following sample code. */
                    out.println("{\"result\":\"true\"}");
                } catch (Exception e) {
                    out.println("{\"result\":\"false\"}");
                }

            } catch (Exception e) {
                e.printStackTrace();
                tx.rollback();
                out.println("{\"result\":\"false\"}");
            } finally {
                out.close();
                session.close();
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
